﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for ProgressbarWindow.xaml
    /// </summary>
    public partial class ProgressbarWindow : UserControl
    {
        private Window mainWindow;
        private ProgressbarWindow()
        {
            InitializeComponent();
            mainWindow = new Window();
            mainWindow.Content = this;
            mainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            mainWindow.WindowStyle = WindowStyle.None;
            mainWindow.Topmost = true;
            mainWindow.ShowInTaskbar = false;
            mainWindow.AllowsTransparency = true;
            mainWindow.Background = new SolidColorBrush(Colors.Transparent);
            mainWindow.Closing += MainWindow_Closing;
            mainWindow.Height = 40;
            mainWindow.Width = 300;
        }

        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            mainWindow.Hide();
            _IsHasShowed = false;
            e.Cancel = true;
        }

        private bool _IsHasShowed = false;

        private static ProgressbarWindow _Instance;
        public static ProgressbarWindow Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new ProgressbarWindow();
                }
                return _Instance;
            }
        }

        public void ShowProgress(bool isIndeterminate)
        {
            Dispatcher.Invoke(() =>
            {
                _ctlProgressbar.IsIndeterminate = isIndeterminate;
                _ctlProgressbar.Maximum = 100;
                _ctlProgressbar.Minimum = 0;
                _ctlProgressbar.Value = 0;
                if (!_IsHasShowed)
                {
                    _IsHasShowed = true;
                    mainWindow.ShowDialog();
                }
            });
        }

        public void ReportProgress(double current, double total)
        {
            if (current <= total)
            {
                Dispatcher.Invoke(() =>
                {
                    _ctlProgressbar.Maximum = total;
                    _ctlProgressbar.Value = current;
                });
            }
        }

        public void Complete()
        {
            Dispatcher.Invoke(() =>
            {
                mainWindow.Hide();
            });
            _IsHasShowed = false;
        }
    }
}
